package setting

import (
	ini "clipboard-tray/common/ini"
	log "clipboard-tray/common/log"
	processClipBoard "clipboard-tray/process/clipboard"

	"os"
	"path/filepath"
)

type ConfProcessor struct {
	confPath string
	logger   *log.Log
}

func NewConfProcessor(logger *log.Log) *ConfProcessor {
	dir, _ := os.Executable()
	baseDir := filepath.Dir(dir)
	return &ConfProcessor{confPath: filepath.Join(baseDir, "./setting/conf.ini"), logger: logger}
}

func (c *ConfProcessor) Load() (cfg *Config) {
	cfg = newConfig()
	if err := ini.LoadINI(c.confPath, cfg); err != nil {
		c.logger.Warn("No valid config file, use default config now:%s", err.Error())
	}
	cfg.Board.Validate()
	return
}

type Config struct {
	Board processClipBoard.ClipBoardConfig `ini:"clipboard"`
}

// 默认配置
func newConfig() *Config {
	return &Config{
		Board: *processClipBoard.NewClipBoardConfig(),
	}
}
